package org.zjx.team;

import lombok.Data;
import org.zjx.battle.BattleManager;
import org.zjx.common.model.RouteProperties;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Data
public abstract class AbstractTeam implements ITeam {
    private String teamId;
    private int maxMembers;
    private TeamType teamType;
    private Formation formation;
    private List<String> teamMembers = new ArrayList<>();
    private String leader;

    public AbstractTeam(int maxMembers, TeamType teamType, String leader) {
        this.teamId = UUID.randomUUID().toString();
        this.maxMembers = maxMembers;
        this.teamType = teamType;
        this.formation = Formation.NORMAL;
        this.leader = leader;
        teamMembers.add(leader);
    }

    @Override
    public boolean addTeamMember(String playerId) {
        if (isInTeam(playerId) && !isVacancy()) {
            return false;
        } else if (isInTeam(playerId)) {
            teamMembers.add(playerId);
        }
        return true;
    }

    @Override
    public void removeTeamMember(String playerId) {
        teamMembers.remove(playerId);
    }

    @Override
    public boolean isVacancy() {
        return teamMembers.size() < maxMembers;
    }

    @Override
    public boolean isInTeam(String playerId) {
        return !teamMembers.contains(playerId);
    }

    @Override
    public boolean allPlayersJoined() {
        if (isVacancy()) return false;
        for (String playerId : teamMembers) {
            RouteProperties routeProperties = BattleManager.getInstance().getRouteProperties(playerId);
            if (routeProperties == null || !routeProperties.isInTeam()) return false;
        }
        return true;
    }
}
